package com.jandan.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.jandan.logic.JWordzFacade;
import com.jandan.ui.client.util.ClientUtil;
import com.jandan.ui.model.CnAndSentence;
import com.jandan.ui.model.Word;
/**
 * 
 * 用于处理单词操作的ajax请求请求，返回Json格式数据，请求路径在json-servlet.xml中配置，关于所使用的json处理包，参见相关文档
 * 这个类主要用于后台的一些请求
 * @author Gong Yong
 * @version 1.0
 *
 */
public class WordController implements Controller {
	private JWordzFacade jwordz;

	public void setJwordz(JWordzFacade jwordz) {
		this.jwordz = jwordz;
	}

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		String action = request.getParameter("action");

		if (action.equalsIgnoreCase("deleteCs") == true) {
			String csID = request.getParameter("csID");
			if (csID != null) {
				long c = Long.parseLong(csID);
				jwordz.deleteCnAndSentence(c);
			}
		} else if (action.equals("addAndEdit")) {
			String wordEn = request.getParameter("wordEn");
			String wordID = request.getParameter("wordID");
			String phonetics = request.getParameter("phonetics");
			String description = request.getParameter("description");
			String csIDs = request.getParameter("csIDs");
			String cns = request.getParameter("cns");
			String ss = request.getParameter("ss");
			long wordLibID = Long.parseLong(request.getParameter("wordLibID"));

			if (csIDs != null) {
				String[] csIDArr = csIDs.split("#");
				String[] cnArr = cns.split("#");
				String[] senArr = ss.split("#");

				List<CnAndSentence> csList = new ArrayList<CnAndSentence>();
				int length = csIDArr.length; 
				CnAndSentence cs = null;
				long w = 0;
				Word word = null;
				for (int i = 0; i < length; i++) {
					cs = new CnAndSentence();
					cs.setCsID(Long.parseLong(csIDArr[i]));
					cs.setCn(cnArr[i]);
					if (senArr.length > i)
						cs.setSentence(senArr[i]);

					csList.add(cs);
				}

				word = new Word();
				w = Long.parseLong(wordID);
				if (w > 0) {
					word.setWordID(w);
				}
				word.setWordEn(wordEn);
				word.setPhonetics(phonetics);
				word.setCnAndSentenceList(csList);
				word.setDescription(description);

				word.setWordLibID(wordLibID);
				if (w < 0) {
					jwordz.insertWord(word);
				} else {
					jwordz.updateWord(word);
				}
			}
		} else if (action.equalsIgnoreCase("search")) {
			String wordEn = request.getParameter("wordEn");
			if (wordEn != null) {
				Word word = jwordz.getWordByWordEnAndWordLibID(wordEn,
						ClientUtil.STANDARD_WORDLIB_ID);
				if (word != null) {
					List<CnAndSentence> csList = word.getCnAndSentenceList();

					StringBuffer sb = new StringBuffer();
					sb.append("<ul>");
					
					CnAndSentence cs = null;
					String cn = null;
					String ss = null;
					for (Iterator<CnAndSentence> it = csList.iterator(); it
							.hasNext();) {
						cs = it.next();
						cn = cs.getCn();
						ss = cs.getSentence();

						if (cn != null) {
							sb.append("<li>");
							sb.append("<span>" + cn + "<span>");
							if (ss != null && !ss.equalsIgnoreCase("none")) {
								sb.append("<p>" + ss + "</p>");
							}
							sb.append("</li>");
						}
					}
					sb.append("</ul>");

					Map map = new HashMap();
					map.put("wordID", word.getWordID());
					map.put("wordEn", word.getWordEn());
					map.put("phonetics", word.getPhonetics());
					map.put("cns", sb.toString());

					return new ModelAndView("jsonView", map);
				}
			}
		}

		return null;
	}

}
